package com.x.jsp;

import jakarta.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * ClassName: JspController
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2024.09.12 22:45
 */

@Controller
@RequestMapping("jsp")
public class JspController {

    /**
     * 不能加@ResponseBody，此注解意味着不走视图解析器，不寻找视图资源
     * 返回值为String类型，返回值为对应拼成的字符串的中间部分
     */

    @RequestMapping("index")
    public String data(HttpServletRequest request){
        request.setAttribute("data","should be red");//嵌入jsp标签中
        return "index";
    }

    /**
     * 请求转发：
     *      1、方法返回值为String
     *      2、不能添加@ResponseBody
     *      3、在返回的字符串前，添加forward:/.../...路径*/


    @GetMapping("forward")
    public String forward(){
        return "forward:/jsp/index";
    }


    /**
     * 请求转发：
     *      1、方法返回值为String
     *      2、不能添加@ResponseBody
     *      3、在返回的字符串前，添加redirect:/.../...路径*/

    @GetMapping("redirect")
    public String redirect(){
        return "redirect:/jsp/index";
    }

    @GetMapping("redirect/baidu")
    public String redirectBaidu(){
        return "redirect:http://www.baidu.com";
    }
}
